package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/order")
@RestController
public class OrderController {
    
    @Autowired
    private OrderService orderService;


    //page: this.page,
    // pageSize: this.pageSize,
    // number: this.input || undefined, beginTime: this.beginTime || undefined, endTime: this.endTime || undefined
    @GetMapping("/page")
    public R findPage(Integer page,Integer pageSize,Long number,String beginTime,String endTime){
        /*System.out.println("page = " + page);
        System.out.println("pageSize = " + pageSize);
        System.out.println("number = " + number);
        System.out.println("beginTime = " + beginTime);
        System.out.println("endTime = " + endTime);
        System.out.println("================================");*/

        try {
            IPage<Orders> iPage= orderService.findPage(page,pageSize,number,beginTime,endTime);
           return R.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
           return R.error("分页查询失败");
        }
    }

    /**
     * 移动端--分页查询订单
     * @param pageParam  分页参数
     * @return  当前页查询到的数据
     */
    @GetMapping("/userPage")
    public R<IPage<OrdersDto>> getList(PageParam pageParam){
        System.out.println("pageParam === " + pageParam);
        try {
            IPage<OrdersDto> page = orderService.getList(pageParam);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询订单失败！");
        }
    }

    
    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R add(@RequestBody Orders orders , HttpSession session){
        
        //1. 调用service
        User user = (User) session.getAttribute("user");
        int row = orderService.add(orders ,  user);
        
        //2. 判定
        if(row >0 ){
            return R.success("提交订单成功！");
        }else{
            return R.error("提交订单失败！");
        }
    }
}
